package com.borya.action.mobile;

import com.alibaba.fastjson.JSONObject;
import com.borya.action.BaseAction;
import com.borya.framework.annotation.Action;
import com.borya.framework.annotation.Autowired;
import com.borya.framework.annotation.JsonResult;
import com.borya.framework.annotation.UrlMapping;
import com.borya.framework.core.ServerRequest;
import com.borya.framework.core.ServerResponse;
import com.borya.model.db.User;
import com.borya.service.mobile.UserService;
import com.borya.statuscode.StatusCode;
import com.borya.util.StringUtil;
import org.apache.log4j.Logger;

@UrlMapping("/c/user/")
@Action
public class UserAction extends BaseAction {

	private Logger log = Logger.getLogger(getClass());
	@Autowired("userServiceImpl")
	private UserService userService;

	@UrlMapping("login")
	@JsonResult
	public String login(ServerRequest request, ServerResponse response) {
		JSONObject json = getJson(request);
		String result = userService.login(json, request.ip());
		log.info("INFO[" + json + "],OUT[" + result + "]");
		return result;
	}

	@UrlMapping("loginout")
	@JsonResult
	public String logout(ServerRequest request, ServerResponse response) {
		JSONObject json = getJson(request);
		String result = userService.loginout(json);
		log.info("INFO[" + json + "],OUT[" + result + "]");
		return result;
	}

	/**
	 * 登录获取验证码
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@UrlMapping("getSmsCode")
	@JsonResult
	public String getSmsCode(ServerRequest request, ServerResponse response) {
		JSONObject json = getJson(request);
		String result = userService.getSmsCode(json);
		log.info("INFO[" + json + "],OUT[" + result + "]");
		return result;
	}

	@UrlMapping("userInfo")
	@JsonResult
	public String userInfo(ServerRequest request, ServerResponse response) {
		JSONObject json = getJson(request);
		User user = getUser(request);
		String result = userService.userInfo(user, json);
		log.info("INFO[" + json + "],OUT[" + result + "]");
		return result;
	}

	@UrlMapping("updateUser")
	@JsonResult
	public String updateUser(ServerRequest request, ServerResponse response) {
		JSONObject json = getJson(request);
		User user = getUser(request);
		String result = userService.updateUser(user, json);
		log.info("INFO[" + json + "],OUT[" + result + "]");
		return result;
	}

	@UrlMapping("userSign")
	@JsonResult
	public String signAgeeement(ServerRequest request, ServerResponse response) {
		JSONObject json = getJson(request);
		User user = getUser(request);
		String result = userService.signAgeeement(user, json);
		log.info("INFO[" + json + "],OUT[" + result + "]");
		return result;
	}

	@UrlMapping("getUserList")
	@JsonResult
	public String getUserList(ServerRequest request, ServerResponse response) {
		JSONObject json = getJson(request);
		User user = getUser(request);
		String result = userService.getUserList(user, json);
		log.info("INFO[" + json + "],OUT[" + result + "]");
		return result;
	}

    @UrlMapping("getUserDetailList")
    @JsonResult
    public String getUserDetailList(ServerRequest request, ServerResponse response) {
        JSONObject json = getJson(request);
        User user = getUser(request);
        String result = userService.getUserDetailList(user, json);
        log.info("INFO[" + json + "],OUT[" + result + "]");
        return result;
    }

    @UrlMapping("getManageLogList")
    @JsonResult
    public String getManageLogList(ServerRequest request, ServerResponse response) {
        JSONObject json = getJson(request);
        User user = getUser(request);
        String result = userService.getManageLogList(user, json);
        log.info("INFO[" + json + "],OUT[" + result + "]");
        return result;
    }

	@UrlMapping("getBindPhoneCode")
	@JsonResult
	public String getBindPhoneCode(ServerRequest request, ServerResponse response) {
		JSONObject json = getJson(request);
		User user = getUser(request);
		String result = userService.getBindPhoneCode(user, json);
		log.info("INFO[" + json + "],OUT[" + result + "]");
		return result;
	}

	@UrlMapping("bindPhone")
	@JsonResult
	public String bindPhone(ServerRequest request, ServerResponse response) {
		JSONObject json = getJson(request);
		User user = getUser(request);
		String result = userService.bindPhone(user, json);
		log.info("INFO[" + json + "],OUT[" + result + "]");
		return result;
	}

	@UrlMapping("removeUser")
	@JsonResult
	public String removeUser(ServerRequest request, ServerResponse response) {
		JSONObject json = getJson(request);
		User user = getUser(request);
		String result = userService.removeUser(user, json);
		log.info("INFO[" + json + "],OUT[" + result + "]");
		return result;
	}

	@UrlMapping("getCreateCode")
	@JsonResult
	public String getCreateCode(ServerRequest request, ServerResponse response) {
		JSONObject json = getJson(request);
		User user = getUser(request);
		String result = userService.getCreateCode(user, json);
		log.info("INFO[" + json + "],OUT[" + result + "]");
		return result;
	}

	@UrlMapping("createUser")
	@JsonResult
	public String createUser(ServerRequest request, ServerResponse response) {
		JSONObject json = getJson(request);
		User user = getUser(request);
		String result = userService.createUser(user, json);
		log.info("INFO[" + json + "],OUT[" + result + "]");
		return result;
	}

    @UrlMapping("getUploadImgConfig")
    @JsonResult
    public String getUploadImgConfig(ServerRequest request, ServerResponse response) {
        JSONObject json = getJson(request);
        String result = userService.getUploadImgConfig(json);
        log.info("INFO[" + json + "],OUT[" + result + "]");
        return result;
    }

	/**
	 * 商户自注册
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@UrlMapping("getCode")
	@JsonResult
	public String getCode(ServerRequest request, ServerResponse response) {
		JSONObject json = getJson(request);
		String result = userService.getCode(json);
		log.info("INFO[selfRegisteringGetCode" + json + "],OUT[" + result + "]");
		return result;
	}

	@UrlMapping("createVisitor")
	@JsonResult
	public String createVisitor(ServerRequest request, ServerResponse response) {
		JSONObject json = getJson(request);
		String result = userService.createVisitor(json);
		log.info("INFO[selfRegisteringCheckPhone" + json + "],OUT[" + result + "]");
		return result;
	}

	@UrlMapping("uploadImg")
	@JsonResult
	public String uploadImg(ServerRequest request, ServerResponse response) {
		JSONObject json = getJson(request);
		User user = getUser(request);
		log.info("URL:%s，请求参数：%s, /c/user/uploadImg");
		String result = userService.uploadImg(user, json);
		json.put("img", "");
		log.info("OUT[" + result + "]");
		System.gc();
		return result;
	}

	@UrlMapping("selfRegistering")
	@JsonResult
	public String selfRegistering(ServerRequest request, ServerResponse response) {
		JSONObject json = getJson(request);
		User user = getUser(request);
		// final String boryaAuthStr = json.getString("boryaAuth");
		// final int backgroundVersion = json.getIntValue("backgroundVersion");
		// HeaderEntity authHeader = JSONObject.parseObject(boryaAuthStr,
		// HeaderEntity.class);
		String merchantType = json.getString("merchantType");
		log.info("INFO[" + json + "]");
		if (StringUtil.isNULL(merchantType)) {
			log.warn("param merchantType error");
			return StatusCode.PARAM_ERROR.toJSON("参数 商户属性 错误");
		}
		if (StringUtil.isNULL(json.getString("userName"))) {
			log.warn("param userName error");
			return StatusCode.PARAM_ERROR.toJSON("参数 用户名 错误");
		}
		if (StringUtil.isNULL(json.getString("idCard"))) {
			log.warn("param idCard error");
			return StatusCode.PARAM_ERROR.toJSON("参数 证件号 错误");
		}
		if (StringUtil.isNULL(json.getString("cityCode"))) {
			log.warn("param cityCode error");
			return StatusCode.PARAM_ERROR.toJSON("参数 城市code 错误");
		}
		if (StringUtil.isNULL(json.getString("cityName"))) {
			log.warn("param cityName error");
			return StatusCode.PARAM_ERROR.toJSON("参数 城市名 错误");
		}
		if (null == json.getString("address")) {
			log.info("param address error");
			return StatusCode.PARAM_ERROR.toJSON("证件地址不存在");
		}
		if (StringUtil.isNULL(json.getString("imgNo1"))) {
			log.warn("param imgNo1 error");
			return StatusCode.PARAM_ERROR.toJSON("参数 门店照片左 错误");
		}
		if (StringUtil.isNULL(json.getString("imgNo2"))) {
			log.warn("param imgNo2 error");
			return StatusCode.PARAM_ERROR.toJSON("参数 门店照片右 错误");
		}
		if (StringUtil.isNULL(json.getString("imgNo3"))) {
			log.warn("param imgNo3 error");
			return StatusCode.PARAM_ERROR.toJSON("参数 手持证件照 错误");
		}
		if (StringUtil.isNULL(json.getString("imgNo4"))) {
			log.warn("param imgNo4 error");
			return StatusCode.PARAM_ERROR.toJSON("参数 签名照片 错误");
		}
		if (json.getString("inviteCode") == null) {
			log.warn("param inviteCode error");
			return StatusCode.PARAM_ERROR.toJSON("参数 邀请码 错误");
		}
		if (StringUtil.isNULL(json.getString("deviceCode"))) {
			log.warn("param deviceCode error");
			return StatusCode.PARAM_ERROR.toJSON("参数 设备号 错误");
		}

		// if (authHeader.getAppVersion() >= backgroundVersion) {
		// if (StringUtil.isNULL(json.getString("shopAddress"))) {
		// log.warn("param shopAddress error");
		// return StatusCode.PARAM_ERROR.toJSON("门店地址不能为空");
		// }
		// if
		// (StringUtil.isNULL(json.getString("shopSpecificAddress").replaceAll("
		// ", ""))) {
		// log.warn("param shopSpecificAddress error");
		// return StatusCode.PARAM_ERROR.toJSON("门店地址不能为空");
		// }
		// if ("1".equals(merchantType)) { // 1为企业商户，企业商户激活时需要检验是否传了身份证相关信息
		// if (StringUtil.isNULL(json.getString("identityCard"))) {
		// log.warn("param identityCard error");
		// return StatusCode.PARAM_ERROR.toJSON("参数 身份证号码 错误");
		// }
		// if (StringUtil.isNULL(json.getString("identityCardName"))) {
		// log.warn("param identityCardName error");
		// return StatusCode.PARAM_ERROR.toJSON("参数 身份证姓名 错误");
		// }
		// if (StringUtil.isNULL(json.getString("identityCardAddress"))) {
		// log.warn("param identityCardAddress error");
		// return StatusCode.PARAM_ERROR.toJSON("参数 身份证地址 错误");
		// }
		// if (StringUtil.isNULL(json.getString("identifyPhotoHead"))) {
		// log.warn("param identifyPhotoHead error");
		// return StatusCode.PARAM_ERROR.toJSON("参数 身份证头像照片 错误");
		// }
		// if (StringUtil.isNULL(json.getString("identifyPhotoFront"))) {
		// log.warn("param identifyPhotoFront error");
		// return StatusCode.PARAM_ERROR.toJSON("参数 身份正面照片 错误");
		// }
		// if (StringUtil.isNULL(json.getString("identifyPhotoReverse"))) {
		// log.warn("param identifyPhotoReverse error");
		// return StatusCode.PARAM_ERROR.toJSON("参数 身份背面照片 错误");
		// }
		// if (StringUtil.isNULL(json.getString("identifyPhotoHand"))) {
		// log.warn("param identifyPhotoHand error");
		// return StatusCode.PARAM_ERROR.toJSON("参数 手持身份证照片 错误");
		// }
		// }
		// } else {
		// log.warn("IOS app 版本低于服务器版本,appVersion=" + authHeader.getAppVersion()
		// + ",serverVersion=" +
		// instance.getInteger(AppSignTokenVersion.KA_MENG_IOS.getToken()));
		// String storeAddressBase = json.getString("storeAddress");
		// if (storeAddressBase != null
		// && storeAddressBase.replaceAll("\r|\n", "").length() < 8) {
		// log.warn("param storeAddress error");
		// return StatusCode.PARAM_ERROR.toJSON("门店地址不存在");
		// }
		// }
		String result = userService.selfRegistering(user, json);
		log.info("INFO[" + json + "],OUT[" + result + "]");
		return result;
	}

	@UrlMapping("checkPowerForModel")
	@JsonResult
	public String checkPowerForModel(ServerRequest request, ServerResponse response) {
		JSONObject json = getJson(request);
		User user = getUser(request);
		String result = userService.checkPowerForModel(user, json);
		log.info("INFO[" + json + "],OUT[" + result + "]");
		return result;
	}

	@UrlMapping("checkDeviceId")
	@JsonResult
	public String checkDeviceId(ServerRequest request, ServerResponse response) {
		JSONObject json = getJson(request);
		User user = getUser(request);
		String result = userService.checkDeviceId(user, json);
		log.info("INFO[" + json + "],OUT[" + result + "]");
		return result;
	}

	@UrlMapping("bannerList")
	@JsonResult
	public String bannerList(ServerRequest request, ServerResponse response) {
		User user = getUser(request);
		String result = userService.bannerList(user);
		log.info("OUT[" + result + "]");
		return result;
	}

	/**
	 * 记录用户签订协议
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@UrlMapping("addProtocolData")
	@JsonResult
	public String addProtocolData(ServerRequest request, ServerResponse response) {
		User user = getUser(request);
		String result = userService.addProtocolData(user);
		log.info("OUT[" + result + "]");
		return result;
	}

	/**
	 * 获取用户是否签订协议
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@UrlMapping("getProtocolData")
	@JsonResult
	public String getProtocolData(ServerRequest request, ServerResponse response) {
		User user = getUser(request);
		String result = userService.getProtocolData(user);
		log.info("OUT[" + result + "]");
		return result;
	}

    /**
     * 获取用户关系类型列表
     * @param request
     * @param response
     * @return
     */
    @UrlMapping("getRelationTypeList")
    @JsonResult
    public String getRelationTypeList(ServerRequest request, ServerResponse response) {
        JSONObject json = getJson(request);
        String result = userService.getRelationTypeList(json);
        log.info("INFO[" + json + "],OUT[" + result + "]");
        return result;
    }

    /**
     * 获取子工号协议数据
     */
    @UrlMapping("getSonJobNumProtocol")
    @JsonResult
    public String getSonJobNumProtocol(ServerRequest request, ServerResponse response) {
        JSONObject json = getJson(request);
        User user = getUser(request);
        String result = userService.getSonJobNumProtocol(user, json);
        log.info("INFO[" + json + "],OUT[" + result + "]");
        return result;
    }

}